<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="smallmuou&#39;s blog">
<meta property="og:url" content="http://smallmuou.gitee.io/blog/page/7/index.html">
<meta property="og:site_name" content="smallmuou&#39;s blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="smallmuou&#39;s blog">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://smallmuou.gitee.io/blog/page/7/"/>





  <title>smallmuou's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">smallmuou's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"><a href="https://github.com/smallmuou">https://github.com/smallmuou</a></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/08/24/2017-08-24-Sublime Text好玩的快捷键/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/08/24/2017-08-24-Sublime Text好玩的快捷键/" itemprop="url">Sublime Text好玩的快捷键</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-24T00:00:00+08:00">
                2017-08-24
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/工具/" itemprop="url" rel="index">
                    <span itemprop="name">工具</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>Sublime Text可谓是文本编辑的神奇，类似于vim，玩遛它并非一朝，可能你也和笔者一样，是一个Sublime Text新手，作为新手，你必须掌握一些神奇的快捷键，这样你才会慢慢爱上它。本文将对一些常用快捷键进行归纳，罗列如下. 当然笔者使用的是MAC系统，因此以下快捷键是针对MAC。</p>
<table>
<thead>
<tr>
<th>功能</th>
<th>快捷键</th>
<th>备注</th>
</tr>
</thead>
<tbody>
<tr>
<td>跳到行首、行尾</td>
<td>cmd + <- or="" -=""></-></td>
</tr>
<tr>
<td>快速选择并编辑同一词组</td>
<td>cmd + d（可连按）</td>
</tr>
<tr>
<td>分屏</td>
<td>cmd + option + 数字</td>
<td>数字表示分屏数目</td>
</tr>
<tr>
<td>向下选行</td>
<td>cmd + l（可连按）</td>
</tr>
<tr>
<td>多行编辑</td>
<td>cmd + shift + l</td>
<td>在选中多行情况下，按此快捷键可以同时编辑</td>
</tr>
<tr>
<td>快速打开文件</td>
<td>cmd + p + 文件</td>
</tr>
<tr>
<td>跳转行</td>
<td>cmd + p + :行</td>
</tr>
<tr>
<td>安装包</td>
<td>cmd + shift + p + install</td>
</tr>
</tbody>
</table>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/08/24/2017-08-24-linux下nginx反向代理配置/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/08/24/2017-08-24-linux下nginx反向代理配置/" itemprop="url">linux下nginx反向代理配置</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-24T00:00:00+08:00">
                2017-08-24
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>关于反向代理服务器，大家应该都有耳闻，形象地说就是增设的”前台”，所有访问都通过这个前台，不能直接访问内部服务器，从而实现了隐藏. 通过反向代理可以实现重定向、负载均衡等.</p>
<h4 id="1-安装-pcre"><a href="#1-安装-pcre" class="headerlink" title="1. 安装 pcre"></a>1. 安装 pcre</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz</span><br><span class="line">tar xzvf pcre-8.41.tar.gz</span><br><span class="line"><span class="built_in">cd</span> pcre-8.41</span><br><span class="line">./configure</span><br><span class="line">make</span><br><span class="line">make install</span><br></pre></td></tr></table></figure>
<p>PS：debain 系可以直接安装 libpcre3-dev；如提示确实c++编译器，<code>yum install -y gcc-c++</code></p>
<h4 id="2-安装-nginx"><a href="#2-安装-nginx" class="headerlink" title="2. 安装 nginx"></a>2. 安装 nginx</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">wget http://nginx.org/download/nginx-1.13.4.tar.gz</span><br><span class="line">tar xzvf nginx-1.13.4.tar.gz</span><br><span class="line"><span class="built_in">cd</span> nginx-1.13.4</span><br><span class="line">./configure --user=nginx --group=nginx --prefix=/usr/<span class="built_in">local</span>/nginx --with-http_stub_status_module --with-http_ssl_module</span><br><span class="line">make</span><br><span class="line">make install</span><br><span class="line">ln -s /usr/<span class="built_in">local</span>/nginx/sbin/nginx /usr/<span class="built_in">local</span>/bin</span><br></pre></td></tr></table></figure>
<h4 id="3-运行nginx"><a href="#3-运行nginx" class="headerlink" title="3. 运行nginx"></a>3. 运行nginx</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx</span><br></pre></td></tr></table></figure>
<p>若遇到找不到 pcre 动态库，则将/usr/local/lib加入系统动态库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">'/usr/local/lib'</span> &gt;&gt; /etc/ld.so.conf.d/local.conf</span><br></pre></td></tr></table></figure>
<p>若遇到”nginx: [emerg] getpwnam(“nginx”) failed”，则进行第4步</p>
<h4 id="4-添加用户组"><a href="#4-添加用户组" class="headerlink" title="4. 添加用户组"></a>4. 添加用户组</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">groupadd nginx</span><br><span class="line">useradd -g nginx nginx</span><br></pre></td></tr></table></figure>
<h4 id="5-配置反向代理"><a href="#5-配置反向代理" class="headerlink" title="5. 配置反向代理"></a>5. 配置反向代理</h4><p>vim /usr/local/nginx/conf/nginx.conf</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">user nginx nginx;</span><br><span class="line">error_log  logs/error.log;</span><br><span class="line">pid        logs/nginx.pid;</span><br><span class="line">...</span><br><span class="line">http &#123;</span><br><span class="line">    ...</span><br><span class="line">    server &#123;</span><br><span class="line">        server_name localhost;</span><br><span class="line">        ...</span><br><span class="line">        location / &#123;</span><br><span class="line">            proxy_pass http://www.baidu.com</span><br><span class="line">        &#125;</span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>配置完后，重新加载<code>nginx -s reload</code></p>
<p>当我访问该代理服务器ip，则会转到<a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a></p>
<h4 id="6-停止-nginx"><a href="#6-停止-nginx" class="headerlink" title="6. 停止 nginx"></a>6. 停止 nginx</h4><p>若需要停止 nginx，则执行如下命令或ps &amp; kill<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">killall nginx</span><br></pre></td></tr></table></figure></p>
<h4 id="7-实例讲解"><a href="#7-实例讲解" class="headerlink" title="7. 实例讲解"></a>7. 实例讲解</h4><p>最后讲一个实用例子，一台主机<a href="http://www.example.com有三个服务，分别对应三个端口" target="_blank" rel="noopener">www.example.com有三个服务，分别对应三个端口</a>(80, 81, 82)，我可以申请3个域名，分别对应这三个服务，配置如下：</p>
<p>test.example.com -&gt; 代理服务器IP<br>test1.example.com -&gt; 代理服务器IP<br>test2.example.com -&gt; 代理服务器IP</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">server &#123;</span><br><span class="line">    server_name test.example.com;</span><br><span class="line">    ...</span><br><span class="line">    location / &#123;</span><br><span class="line">        proxy_pass http://www.example.com:80;</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line">server &#123;</span><br><span class="line">    server_name test1.example.com;</span><br><span class="line">    ...</span><br><span class="line">    location / &#123;</span><br><span class="line">        proxy_pass http://www.example.com:81;</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line">server &#123;</span><br><span class="line">    server_name test2.example.com;</span><br><span class="line">    ...</span><br><span class="line">    location / &#123;</span><br><span class="line">        proxy_pass http://www.example.com:82;</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这样我就可以不用为每个服务购买一台主机</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/08/18/2017-08-18-iptables简述/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/08/18/2017-08-18-iptables简述/" itemprop="url">iptables简述</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-18T00:00:00+08:00">
                2017-08-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>很多linux系统都采用iptables来构建防火墙，因此有必要对iptables进行深入的了解。</p>
<h3 id="4表5链"><a href="#4表5链" class="headerlink" title="4表5链"></a>4表5链</h3><p>iptables 或 netfilter 说白了，就是一堆规则，只有包符合这些规则，才可以通过。而这些规则有着类别和优先级关系，下图给出了这些类别及包在netfilter经过的路径</p>
<p><img src="/images/iptables.jpg" alt="iptables"></p>
<h3 id="iptables-命令"><a href="#iptables-命令" class="headerlink" title="iptables 命令"></a>iptables 命令</h3><p>linux 提供了 iptables 用于编辑这些规则，下面列出 iptables 命令常用的选项</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">-t 				指定 table，默认是 filter</span><br><span class="line">-A				末尾追加规则</span><br><span class="line">-D				删除规则</span><br><span class="line">-L 				列出规则</span><br><span class="line">-R				替换规则</span><br><span class="line">-I 				插入规则</span><br><span class="line">-p 				指定协议，all 表示所有</span><br><span class="line">-v 				详细信息</span><br><span class="line">--line-numbers 	显示行号，方便删除</span><br><span class="line">-s				指定源地址</span><br><span class="line">-d 				指定目的地址</span><br><span class="line">-i 				interfaces, eth0等</span><br><span class="line">-n 				数字化</span><br><span class="line">-j 				动作，ACCEPT、DROP 等</span><br><span class="line">-F 				清空所有规则</span><br></pre></td></tr></table></figure>
<p>为了方便理解，给出一些简单的例子</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#禁止所有主机访问80端口</span><br><span class="line">iptables -t filter -A INPUT -d 192.168.1.108 -p tcp --dport 80 -j REJECT</span><br><span class="line"></span><br><span class="line"># 查看所有规则（filter）</span><br><span class="line">iptables -nvL</span><br><span class="line"></span><br><span class="line"># 清空规则</span><br><span class="line">iptables -F</span><br></pre></td></tr></table></figure>
<p>注意：以上配置都是临时的，可以通过配置/etc/sysconfig/iptables或/etc/init.d/iptables save来固化</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/08/17/2017-08-17-为什么你的广播包收不到/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/08/17/2017-08-17-为什么你的广播包收不到/" itemprop="url">为什么你的广播包收不到</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-17T00:00:00+08:00">
                2017-08-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>‘为什么收不到广播包’，听到的答案都是被路由丢弃了，其实这只是一种情况，而这种情况是比较好理解，一般都会规避，往往实际情况不是这样，比如：</p>
<ul>
<li>同一个路由器下，两台设备的广播包到不了</li>
<li>两台设备网线直连，广播包到不了</li>
</ul>
<p>本文将为大家一一介绍其中的缘由.</p>
<h3 id="1-概念"><a href="#1-概念" class="headerlink" title="1. 概念"></a>1. 概念</h3><p>广播包，即发给所有本地主机的包，分为：</p>
<ul>
<li>受限广播包（255.255.255.255）- 不被转发</li>
<li>子网广播包（netid.1）- 往所有子网广播</li>
<li>主机广播包（netid.subnetid.1）- 往指定子网广播</li>
</ul>
<p>这里有一点需要强调：广播包是基于 UDP ，TCP 是没有广播包的说法</p>
<h3 id="2-用途"><a href="#2-用途" class="headerlink" title="2. 用途"></a>2. 用途</h3><p>广播包的用途自然就不用说，ARP、DHCP等一些非常重要的协议都是基于广播包。当然也不能滥用，否则会影响网络及主机性能，就好比你天天都在小区大喊，谁也受不了。</p>
<h3 id="3-阻碍因子"><a href="#3-阻碍因子" class="headerlink" title="3. 阻碍因子"></a>3. 阻碍因子</h3><p>广播包既然可以发给所有主机，按理说所有主机都需要处理它，那有哪些因素会阻碍『收广播包』？</p>
<h4 id="3-1-路由器"><a href="#3-1-路由器" class="headerlink" title="3.1. 路由器"></a>3.1. 路由器</h4><p>为了保证网络的干净，路由器一般不转发广播包，于是这里大家常常误解，以为只要广播包一接入路由器，都有可能被路由器丢弃；于是往往有个答案『你的广播包被路由器丢弃』，其实只有经过 WAN口的广播包才会丢弃。因此如果是2个 LAN 口间的广播包是不会丢弃的，即使两个主机 IP 不一样。</p>
<p>比如：192.168.1.88与192.168.2.88两台主机连到192.168.2.1路由上照样可以正常收发广播包。</p>
<h4 id="3-2-多网卡（发送方）"><a href="#3-2-多网卡（发送方）" class="headerlink" title="3.2. 多网卡（发送方）"></a>3.2. 多网卡（发送方）</h4><p>默认是不会往所有网卡发送广播包，广播包跟其他数据包一样，也是通过本地路由表来决策该往哪个网卡发送，比如有一台双网卡，分别是61和161网段，有如下路由表：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Kernel IP routing table</span><br><span class="line">Destination     Gateway         Genmask         Flags Metric Ref    Use Iface</span><br><span class="line">...</span><br><span class="line">0.0.0.0         192.168.161.1   0.0.0.0         UG    0      0        0 em1</span><br><span class="line">0.0.0.0         192.168.61.1    0.0.0.0         UG    0      0        0 em2</span><br></pre></td></tr></table></figure>
<p>广播包是往 em1发送，而不会往 em0发送（优先匹配前面规则），当然你也可以指定往哪个网卡发送</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">srcaddr.sin_addr.s_addr=inet_addr(&quot;192.168.61.223&quot;);</span><br></pre></td></tr></table></figure>
<p>实测 window 系统，也是如此.</p>
<p>因此，如果是双网卡，请确保是否发对网卡。</p>
<h4 id="3-3-系统内核过滤（接收方）"><a href="#3-3-系统内核过滤（接收方）" class="headerlink" title="3.3. 系统内核过滤（接收方）"></a>3.3. 系统内核过滤（接收方）</h4><p>还有一种情况，比如 linux 系统增加了一种 Reverse Path Filter，即会对来源 IP 合法性进行判断，即是否本该从该网卡发过来，若不符合，则丢弃。简单地判断就是验证是否可以往相应网口向该 IP 发送数据。</p>
<p>比如你的路由表如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Kernel IP routing table</span><br><span class="line">Destination     Gateway         Genmask         Flags Metric Ref    Use Iface</span><br><span class="line">192.168.161.0   192.168.161.1   255.255.255.0   UG    0      0        0 em1</span><br><span class="line">192.168.161.0   0.0.0.0         255.255.255.0   U     0      0        0 em1</span><br><span class="line">192.168.61.0    0.0.0.0         255.255.255.0   U     0      0        0 em2</span><br></pre></td></tr></table></figure>
<p>则以下情况都是不合法来源</p>
<ul>
<li>在网口 em1，收到了192.168.61.xx的数据包（应该在 em2上收到）</li>
<li>网卡 em1或 em2收到10.10.xx.xx的数据包</li>
</ul>
<p>该机制是为防止恶意攻击，具体各位可以去了解下。那么是否可以关闭该特性，肯定是可以.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/sysctl.conf</span><br><span class="line"></span><br><span class="line">net.ipv4.conf.default.rp_filter = 0</span><br></pre></td></tr></table></figure>
<p>即可，当然也可以增加路由表来解决。</p>
<p>该特性只在 linux 上，window系统就不得而知了。</p>
<h3 id="3-4-VLAN"><a href="#3-4-VLAN" class="headerlink" title="3.4 VLAN"></a>3.4 VLAN</h3><p>为了防止大量广播消耗网络资源，有些大型局域网会使用VLAN技术，将交换机下的某些口组成一个单独的 LAN，这样广播包只会在该 LAN，而不会发到其他端口上.</p>
<h3 id="4-总结"><a href="#4-总结" class="headerlink" title="4. 总结"></a>4. 总结</h3><p>当你遇到广播包收不到问题，可以基于如下思路排查：</p>
<ul>
<li>确认物理连接是否属于统一网段(注意是物理连接，而不是说IP)</li>
<li>确认发送发是否发对网卡</li>
<li>如果是接收方是 linux，确认系统是否开启 rpfilter</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/08/17/2017-08-17-理解MTU和MSS/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/08/17/2017-08-17-理解MTU和MSS/" itemprop="url">理解MTU和MSS</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-17T00:00:00+08:00">
                2017-08-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>遇到个奇怪的问题「无法上传文件至七牛服务器」，依次排除了七牛服务、本地网络，最终定位到只要数据发送超过1440Byte，连路就断了，实际测了下是对方电信出口的限制，具体原因不得而知，解决方法，就是把路由的MTU设小，使得数据小于1440Byte。籍此，对MTU进行了深入学习。</p>
<h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p>MTU(Maximum Transmission Unit) - 一种通信协议的某一层上面所能通过的最大数据包大小.</p>
<p>以太网为1500，这也是为何路由器或PC上默认都是1500，因此数据包一旦超过此大小，就会进行分包，这也就有了IP分片的过程，这里就引入了一个问题，IP包是不可靠的，如果IP分片，如何保证 TCP 可靠？</p>
<p>于是这里引入了 MSS，</p>
<p>MSS(Maximum Segment Size) - 传输控制协议的一个参数，以字节数定义一个计算机或通信设备所能接受的分段的最大数据量. 这是不包含IP头及TCP或UDP头，对于 TCP，MSS=1500-20（IP 头）-20（TCP 头）</p>
<p>TCP 通过 MSS提前进行分段，从而保证数据不会超过 MTU，来保证IP不需要再进行分片。</p>
<h3 id="获取网络-MTU"><a href="#获取网络-MTU" class="headerlink" title="获取网络 MTU"></a>获取网络 MTU</h3><p>如何检测网络各个节点MTU值呢，可以通过ping的不分片发送，需要注意的是1500是除了以太网头的包大小，对于ping，传递的是ICMP包，由以太网头+IP头+ICMP头+数据组成，只要IP头+ICMP头+数据不超过1500即可</p>
<p>window</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ping -f -l 1472 www.baidu.com</span><br></pre></td></tr></table></figure>
<p>PS：1472 + 20（IP头）+8（ICMP头）= 1500</p>
<p>linux<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ping -M do -c 2 -s 1472 www.baidu.com</span><br></pre></td></tr></table></figure></p>
<p>Mac<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ping -D -s 1472 www.baidu.com</span><br></pre></td></tr></table></figure></p>
<p>在PPPOE下，最大MTU是1492，因为外加PPPOE头部（6字节）+PPP头部（2字节）</p>
<p>可以 ping 各个路由来查看相应MTU，当然如果越近路由MTU较小，则无法检测远端路由.</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/08/17/2017-08-17-软链接与硬链接/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/08/17/2017-08-17-软链接与硬链接/" itemprop="url">软链接与硬链接</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-17T00:00:00+08:00">
                2017-08-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>常常搞不清软链接和硬链接的区别，看了inode才正真理解。</p>
<h3 id="从编程角度"><a href="#从编程角度" class="headerlink" title="从编程角度"></a>从编程角度</h3><ul>
<li>软链接 - 指针，原文件删除后，访问就提示无该文件</li>
<li>硬链接 - 引用，原文件删除后，仍然可以正常访问</li>
</ul>
<h3 id="从inode角度"><a href="#从inode角度" class="headerlink" title="从inode角度"></a>从inode角度</h3><ul>
<li>软链接 - 指向不同inode</li>
<li>硬链接 - 指向相同inode</li>
</ul>
<p>inode中有个引用的变量，每次硬链接都会导致引用加1，因此需要删除所有硬链接及原文件，才会真正删除。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/08/08/2017-08-08-linux内核module编译/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/08/08/2017-08-08-linux内核module编译/" itemprop="url">linux内核module编译</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-08T00:00:00+08:00">
                2017-08-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/后端/" itemprop="url" rel="index">
                    <span itemprop="name">后端</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>最近公司服务器系统升级至 centos7，升级后发现原先的 bios 升级工具无法使用，提示:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ERROR : driver &quot;amifldrv&quot; not found</span><br><span class="line">rmmod: ERROR: Module amifldrv_mod is not currently loaded</span><br><span class="line">ERROR : driver &quot;amifldrv&quot; not found</span><br><span class="line">rmmod: ERROR: Module amifldrv_mod is not currently loaded</span><br><span class="line">ERROR : Unable to load driver.</span><br><span class="line">10 - Error: Unable to load driver.</span><br></pre></td></tr></table></figure>
<p>下面简单描述下解决过程：</p>
<ol>
<li>下载对应版本linux内核源码（uname -r）</li>
<li>将源码拷贝至/usr/src/kernels目录下</li>
<li>进入源码目录,依次执行<code>make oldconfig &amp;&amp; make prepare &amp;&amp; make scripts &amp;&amp; make vmlinux</code></li>
<li>下载<a href="!https://github.com/mrwnwttk/afulnx">amifldrv源码</a></li>
<li>执行 make default，之后会生成.o文件</li>
<li>执行insmod .o，如果没有报错，则表示模块编译成功</li>
</ol>
<p>以上是我编译内核 module 的大致经过，中间遇到了很多坑，比如没有<code>make vmlinux</code>导致”no symbol version for struct_module”。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/07/28/2017-07-28-mysql数据迁移/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/07/28/2017-07-28-mysql数据迁移/" itemprop="url">mysql数据迁移</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-28T00:00:00+08:00">
                2017-07-28
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>最近公司需要切换SIP服务器，需要从原数据库迁移数据至新服务器，其中新旧服务器采用的架构不用，数据结构也不同，下面我将描述下整个迁移过程。</p>
<h5 id="步骤1-确认mysql命令行执行方式"><a href="#步骤1-确认mysql命令行执行方式" class="headerlink" title="步骤1. 确认mysql命令行执行方式"></a>步骤1. 确认mysql命令行执行方式</h5><p>可以通过<code>mysql -e &quot;sql语句&quot;</code>直接执行相应的mysql语句，而不必登入mysql. 如:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -uroot -p123456 -e &quot;show databases&quot;</span><br></pre></td></tr></table></figure>
<p>显示所有库</p>
<h5 id="步骤2-创建新库及表"><a href="#步骤2-创建新库及表" class="headerlink" title="步骤2. 创建新库及表"></a>步骤2. 创建新库及表</h5><p>可以通过<code>show create table xxx</code>来查看表的创建语句，并复制到脚本中.</p>
<h5 id="步骤3-从原库下载数据"><a href="#步骤3-从原库下载数据" class="headerlink" title="步骤3. 从原库下载数据"></a>步骤3. 从原库下载数据</h5><p>可以通过mysqldump从原数据库dump数据至本地</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mysqldump -uroot -p123456 -hxxx.xxx.xxx.xxx testdb  testtable &gt; test.sql</span><br><span class="line">``` </span><br><span class="line"></span><br><span class="line">##### 步骤4. 导入sql至本地库</span><br></pre></td></tr></table></figure>
<p>mysql -uroot -p123456 &lt; test.sql<br><code>`</code></p>
<h5 id="步骤5-迁移原表数据至新表"><a href="#步骤5-迁移原表数据至新表" class="headerlink" title="步骤5. 迁移原表数据至新表"></a>步骤5. 迁移原表数据至新表</h5><p>可以通过<code>insert into xxx select from</code>来完成下相应字段迁移</p>
<h5 id="步骤6-清除旧表及临时文件"><a href="#步骤6-清除旧表及临时文件" class="headerlink" title="步骤6. 清除旧表及临时文件"></a>步骤6. 清除旧表及临时文件</h5><h5 id="步骤7-完成"><a href="#步骤7-完成" class="headerlink" title="步骤7. 完成"></a>步骤7. 完成</h5><p>最后，提供下我写的脚本，大家可以查考：<a href="https://github.com/smallmuou/asterisk2ch2o" target="_blank" rel="noopener">https://github.com/smallmuou/asterisk2ch2o</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/07/21/2017-07-21-记spring boot应用运行非常慢问题排查过程/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/07/21/2017-07-21-记spring boot应用运行非常慢问题排查过程/" itemprop="url">记spring boot应用运行非常慢问题排查过程</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-21T00:00:00+08:00">
                2017-07-21
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/后端/" itemprop="url" rel="index">
                    <span itemprop="name">后端</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>项目上遇到一个问题『Spring Boot应用运行非常慢』，一个正常只要运行3分钟的应用在我们的自己服务器上需要运行10多分钟，针对这个问题我们做过了一些尝试</p>
<ul>
<li>更换服务器（硬盘放到其他服务器运行） - 问题依旧</li>
<li>更换操作系统 - 问题不存在</li>
</ul>
<p>根据以上，我们初步断定是运行环境的问题</p>
<p>于是我对CPU、Memory、IO进行了检测，发现都很正常,因此应该是java运行环境引起的。</p>
<p>于是我编写了java的示例程序HelloWorld，在服务器上测试，发现运行速度跟PC有一定的差别，但由于代码过简单，差别不大，当时以为是配置差别，没太多留意，于是我有编写了Spring Boot的HelloWorld，发现启动需要50s左右，而在PC上只需要5s左右，这差别也太大了；</p>
<p>于是我初步猜想是不是Spring Boot的那段代码block，先去Google发现大家提到的random问题，但很不幸，我更换成urandom后，问题依旧；</p>
<p>于是没有办法，我只能从Spring及Spring Boot源码入手，我从Github 下载了相应的源码，并编译，打印了些位置，发现基本定位不出来，因为从打印的情况看，时间不是耗在某个点，而是均匀分别，普遍偏慢；</p>
<p>于是我更换了策略，从java环境入手。本来要更新下jdk版本，在无意间，发现java版本号带有debug字样，如下<code>java -version</code>：</p>
<pre>
openjdk version "1.8.0_51-debug"
OpenJDK Runtime Environment (build 1.8.0_51-debug-b16)
OpenJDK 64-Bit Server VM (build 25.51-b03-debug, mixed mode)
</pre>

<p>于是非常惊喜，我在自己PC上确认了下，并没有带debug字样，这样想来，使用debug来运行确实会导致整体偏慢，因此我使用<code>alternatives --config java</code>将java切换到正式版，之后打印出来的信息如下：</p>
<pre>
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
</pre>

<p>于是，我重新验证了HelloWorld程序，发现速度正常了（7s左右），又验证了我们自己的应用，时间3分钟左右，至此，这个问题终于得到了解决.</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>java程序慢是由于使用了debug版本的java，导致整个java运行速度偏慢.</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2017/07/05/2017-07-05-静态库与动态库优先级顺序/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2017/07/05/2017-07-05-静态库与动态库优先级顺序/" itemprop="url">静态库与动态库优先级顺序</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-05T00:00:00+08:00">
                2017-07-05
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>关于静态库与动态库的区别，大家应该比较清楚；静态库中的所有符号会打包到目标中（应用程序或者库），而动态库的符号定义不会打包进目标中，因此，静态库只存在于编译阶段，而动态库除了编译阶段，还存在于运行阶段。这就是为何系统lib下会存在很多so文件。那么有这样一个问题，假如编译时同时存在静态库与动态库，那么到底优先链哪个，下面我们通过实验证明下。</p>
<h3 id="1-生成静态库"><a href="#1-生成静态库" class="headerlink" title="1. 生成静态库"></a>1. 生成静态库</h3><p>vim hello.c<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">void hello() &#123;</span><br><span class="line">    printf(&quot;hello static\n&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>gcc -o hello.o -c hello.c<br>ar rc libhello.a hello.o</p>
<h3 id="2-生成动态库"><a href="#2-生成动态库" class="headerlink" title="2. 生成动态库"></a>2. 生成动态库</h3><p>vim hello.c<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">void hello() &#123;</span><br><span class="line">    printf(&quot;hello shared\n&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>gcc hello.c -fPIC -shared -o libhello.so</p>
<h3 id="3-测试"><a href="#3-测试" class="headerlink" title="3. 测试"></a>3. 测试</h3><p>vim main.c<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">extern void hello();</span><br><span class="line">int main() &#123;</span><br><span class="line">    hello();</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>gcc -o main main.c -L. -lhello</p>
<p>实际输出: hello shared</p>
<hr>
<p>总结：从实际例子可以确定，gcc优先采用动态库链接</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/blog/page/6/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/blog/">1</a><span class="space">&hellip;</span><a class="page-number" href="/blog/page/6/">6</a><span class="page-number current">7</span><a class="page-number" href="/blog/page/8/">8</a><span class="space">&hellip;</span><a class="page-number" href="/blog/page/20/">20</a><a class="extend next" rel="next" href="/blog/page/8/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">smallmuou</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">197</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/smallmuou" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-globe"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">smallmuou</span>

  
</div>

    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <span id="busuanzi_container_site_pv">总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
